#include "ObstaclesPlace.h"
#include "../mainScreen/CommonDefinition.h"

ObstaclesPlace::ObstaclesPlace()
{

}

ObstaclesPlace::~ObstaclesPlace()
{

}

ObstaclesPlace* ObstaclesPlace::create(cocos2d::ValueVector &obstaclesPos)
{
	ObstaclesPlace *ret = new (std::nothrow) ObstaclesPlace();
	if (ret && ret->init(obstaclesPos))
	{
		ret->autorelease();
		return ret;
	}
	else
	{
		CC_SAFE_DELETE(ret);
		return nullptr;
	}
}

bool ObstaclesPlace::init(cocos2d::ValueVector &obstaclesPos)
{
	if (cocos2d::Node::init()){

		for (auto& obj : obstaclesPos)
		{
			cocos2d::ValueMap& dict = obj.asValueMap();

			float x = dict["x"].asFloat();
			float y = dict["y"].asFloat();
			float width = dict["width"].asFloat();
			float height = dict["height"].asFloat();

			cocos2d::Node *tempNode = cocos2d::Node::create();

			cocos2d::PhysicsBody *tempBody = cocos2d::PhysicsBody::createBox(cocos2d::Size(width,height));
			tempBody->setCategoryBitmask(OBSTACLES_COLLISION_MASK);
			tempBody->setCollisionBitmask(OBSTACLES_COLLISION_MASK);
			tempBody->setContactTestBitmask(CONTACT_MASK);
			tempBody->setTag(OBSTACLES_PHYBODY_TAG);
			tempBody->setDynamic(false);
			tempBody->setPositionOffset(cocos2d::Vec2(width / 2, height / 2));

			tempNode->setPhysicsBody(tempBody);
			tempNode->setPosition(x, y);
			addChild(tempNode);
		}
		return true;
	}
	return false;
}
